package com.cole.gx095.controller;


import com.cole.gx095.Repository.SaleGroupRepository;
import com.cole.gx095.service.SaleGroupService;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.controller.utils.RedisUtil;
import com.cole.gx095.entity.SaleGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/saleGroup")
@Transactional
public class saleGroupController {
    @Autowired
    SaleGroupRepository saleGroupRepository;
    @Autowired
    SaleGroupService saleGroupService;
    @GetMapping("/List")
    CommonRep List(Integer currentPage,Integer pageSize){
        Page<SaleGroup> page1 = saleGroupRepository.findAll(PageRequest.of(currentPage - 1, pageSize));
        return CommonRep.ok()
                .data("page",page1);
    }
    @PostMapping("/update")
    CommonRep update(@RequestBody SaleGroup saleGroup){
        System.out.println("saleGroup = " + saleGroup);
        if(saleGroup.getId() == null)
            saleGroup.setCreateAdminTrueName(RedisUtil.mget("LoginName"));
        saleGroupRepository.save(saleGroup);
        return CommonRep.ok();
    }
    @PostMapping("/query")
    CommonRep query(Integer currentPage,Integer pageSize, SaleGroup saleGroup){
        System.out.println("saleGroup = " + saleGroup);
        Page<SaleGroup> page1 = saleGroupService.findAllBy(saleGroup, currentPage, pageSize);
        return CommonRep.ok()
                .data("page",page1);
    }
}
